package com.bosintek.wordtest.util;

import android.app.Activity;
import android.content.SharedPreferences;

/**
 * 
 * This class handles everything that is related to data storage.
 * 
 * @author fjrd84
 * 
 */
public class DataManager {
	// Data file.
	public static final String PREFS_NAME = "WTestData";
	
	private Activity act;
	
	public DataManager(Activity act){
		this.act = act;
	}

	public SharedPreferences getPreferences() {
		// Restore preferences
		SharedPreferences settings = act.getSharedPreferences(PREFS_NAME, 0);
		return settings;
	}

	public void setUser(String value) {
		setStr("user",value);
	}
	
	public String getUser(){
		return getStr("user");
	}
	
	public void setEmail(String value) {
		setStr("email",value);
	}
	
	public String getEmail(){
		return getStr("email");
	}
	
	public void setPass(String value) {
		setStr("pass",value);
	}
	
	public String getPass(){
		return getStr("pass");
	}
	
	public void setBanksXML(String value) {
		setStr("banks",value);
	}
	
	public String getBanksXML(){
		return getStr("banks");
	}

	private String getStr(String prefName) {
		// Restore preferences
		SharedPreferences settings = act.getSharedPreferences(PREFS_NAME, 0);
		String name = settings.getString(prefName, "");
		if(name.equals(null)){
			name = "";
		}
		
		return name;
	}
	
	private void setStr(String prefName, String value ){
		// Restore preferences
		SharedPreferences settings = act.getSharedPreferences(PREFS_NAME, 0);

		// We need an Editor object to make preference changes.
		SharedPreferences.Editor editor = settings.edit();
		editor.putString(prefName, value);
		
		// Commit the edits!
		editor.commit();
	}
	
	private int getInt(String prefName) {
		// Restore preferences
		SharedPreferences settings = act.getSharedPreferences(PREFS_NAME, 0);
		int value = settings.getInt(prefName, -1);
		
		return value;
	}
	
	private void setInt(String prefName, int value ){
		// Restore preferences
		SharedPreferences settings = act.getSharedPreferences(PREFS_NAME, 0);
		
		// We need an Editor object to make preference changes.
		SharedPreferences.Editor editor = settings.edit();
		editor.putInt(prefName, value);
		
		// Commit the edits!
		editor.commit();
	}
	
	private boolean getBooleanPref(String prefName) {
		// Restore preferences
		SharedPreferences settings = act.getSharedPreferences(PREFS_NAME, 0);
		boolean value = settings.getBoolean(prefName, false);
		
		return value;
	}
	
	private void setBooleanPref(String prefName, boolean value ){
		// Restore preferences
		SharedPreferences settings = act.getSharedPreferences(PREFS_NAME, 0);

		// We need an Editor object to make preference changes.
		SharedPreferences.Editor editor = settings.edit();
		editor.putBoolean(prefName, value);
		
		// Commit the edits!
		editor.commit();
	}
	
}
